<template>
  <el-card class="box-card">
    <h2 class="titleHeader">
      修改前端首页信息
    </h2>
    <div class="frontHome">
      <el-form label-position="left" label-width="70px" :model="frontHomeForm" @submit.native.prevent>

        <el-form-item label="打 字 机">
          <el-input v-model="frontHomeForm.typewriter" />
        </el-form-item>

        <el-form-item label="公 告 栏">
          <el-input type="textarea" v-model="frontHomeForm.bulletin"></el-input>
        </el-form-item>

        <el-form-item label="底 部 栏">
          <el-input type="textarea" v-model="frontHomeForm.footer">
          </el-input>
        </el-form-item>

        <el-form-item>
          <div style="text-align: right; width: 100%;">
            <button @click="onSubmit" class="submitBtn">
              修 改
            </button>
          </div>
        </el-form-item>

        <el-form-item label="背 景 图">
          <div class="bgUpdate">
            <el-upload ref="upload" class="upload-demo" drag :action="`${store.state.BaseUrl}endIndex/frontImages`"
              style="width: 300px;" :show-file-list="false" :on-success="handleSuccess">
              <el-icon class="el-icon--upload"><upload-filled /></el-icon>
              <div class="el-upload__text">
                将文件拖到此处，或<em>点击上传</em>
              </div>
              <template #tip>
                <div class="el-upload__tip">
                  尽量小一点的图片为了首屏响应
                </div>
              </template>
            </el-upload>
            <el-image style="width: 320px;height: 196px; margin-left: 30px;" :src="frontBg" fit="cover" />
          </div>

        </el-form-item>

        <el-form-item label="底 部 码">
          <div class="bgUpdate">
            <el-upload ref="upload" class="upload-demo" drag :action="`${store.state.BaseUrl}endIndex/footerCode`"
              style="width: 300px;" :show-file-list="false" :on-success="handleSuccessFooter">
              <el-icon class="el-icon--upload"><upload-filled /></el-icon>
              <div class="el-upload__text">
                将文件拖到此处，或<em>点击上传</em>
              </div>
            </el-upload>
            <el-image style="width: 200px;height: 200px; margin-left: 30px;" :src="footerCode" fit="contain" />
          </div>

        </el-form-item>


      </el-form>
    </div>
  </el-card>
</template>

<script lang="ts" setup>
import { reactive, inject, ref } from 'vue'

import { UploadFilled } from '@element-plus/icons-vue'
import type { UploadFile, UploadFiles } from 'element-plus'
import { ElMessage } from 'element-plus'
import { getFrontData, updateEndIndexData } from '@/utils/frontHome'


const store: any = inject('store')

const frontHomeForm = reactive({
  typewriter: '',
  bulletin: '',
  footer: ''
})

// 图片上传成功后的回调
const frontBg = ref('')
const handleSuccess = (res: any, uploadFile: UploadFile, uploadFiles: UploadFiles) => {
  const reader = new FileReader();
  reader.readAsDataURL(uploadFile.raw as any);
  reader.onload = () => {
    const src = reader.result;
    // 这里的reader.result就是文件的base64了。如果是图片的话，就可以直接放到src中展示了
    frontBg.value = src as string
  };
}
// 图片上传成功后的回调 底部码
const footerCode = ref('')
const handleSuccessFooter = (res: any, uploadFile: UploadFile, uploadFiles: UploadFiles) => {
  const reader = new FileReader();
  reader.readAsDataURL(uploadFile.raw as any);
  reader.onload = () => {
    const src = reader.result;
    // 这里的reader.result就是文件的base64了。如果是图片的话，就可以直接放到src中展示了
    footerCode.value = src as string
  };
}

// 提交修改按钮 
const onSubmit = () => {
  if (!frontHomeForm.typewriter.trim() || !frontHomeForm.bulletin.trim() || !frontHomeForm.footer.trim())
    return ElMessage.error('不能为空');

  updateEndIndexData(frontHomeForm.typewriter, frontHomeForm.bulletin, frontHomeForm.footer).then(res => {
    if (res.data.ok) {
      ElMessage.success(res.data.info)
    } else {
      ElMessage.error(res.data.info)
    }
  }).catch(err => ElMessage.error('服务器错误请检查token'))
}

// 获取数据 
getFrontData().then(res => {
  frontHomeForm.bulletin = res.data.info[0].bulletin
  frontHomeForm.typewriter = res.data.info[0].typewriter
  frontHomeForm.footer = res.data.info[0].footer
  frontBg.value = store.state.BaseUrl + res.data.info[0].bg
  footerCode.value = store.state.BaseUrl + 'frontUploads/footerCode.jpg'
})

</script>

<style lang="scss" scoped>
.frontHome {
  width: 80%;
  padding: 30px;
  margin: 20px auto;
}

.bgUpdate {
  width: 100%;
  display: flex;
}

.submitBtn {
  font-size: 14px;
  padding: 8px 20px;
  border: transparent;
  box-shadow: 2px 2px 4px rgba(0, 0, 0, 0.4);
  background: dodgerblue;
  color: white;
  border-radius: 5px;

  &:hover {
    background: rgb(2, 0, 36);
    background: linear-gradient(90deg, rgba(30, 144, 255, 1) 0%, rgba(0, 212, 255, 1) 100%);
  }

  &:active {
    transform: translate(0em, 0.2em);
  }
}
</style>